# -*- coding: utf-8 -*-
"""
Created on Thu Mar 16 13:17:19 2023

@author: maomao
"""

import xlrd
import csv
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.interpolate import make_interp_spline 
from sklearn.model_selection import train_test_split
#设置路径
path='C://Users//maomao//Desktop//计算碳排放驱动因素之间的相关性2.xls'
#读取文件
df = pd.read_excel(path)
print(df)
 
#df.info()
 
#训练随机森林模型
from sklearn.ensemble import RandomForestClassifier
x, y = df.iloc[:, 0:].values, df.iloc[:, 0].values
print("x=",x)
print("y=",y)
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.3, random_state = 0)
print("x_train=",x_train)
print(" x_test=", x_test)
print(" y_train=", y_train)
print("y_test=",y_test)
feat_labels = df.columns[0:]#获取特征名称
print('feat_labels=',feat_labels)
forest = RandomForestClassifier(n_estimators=50, random_state=1, n_jobs=-1,max_depth=3)
forest.fit(x_train, y_train.astype(int))
 
 
#打印特征重要性评分
importances = forest.feature_importances_
print("importances=",importances)
#indices = np.argsort(importances)[::-1]
imp=[]
print("x_train.shape[1]=",x_train.shape[1])
f=0
for f in range(x_train.shape[1]):
    print(f+1, feat_labels[f], importances[f])

  
 
 
#将打印的重要性评分copy到featureScore.xlsx中；plot特征重要性
#设置路径
path='C://Users//maomao//Desktop//计算碳排放驱动因素之间的相关性2.xls'
#打开文件
myBook=xlrd.open_workbook(path)
#查询工作表
sheet_1_by_index=myBook.sheet_by_index(0)
data=[]
for i in range(0,sheet_1_by_index.nrows):
    data.append(sheet_1_by_index.row_values(i))   
data=np.array(data)
X=data[:1,].ravel()
print ("X=",X)
#y=data[1:,]
#print("y=",y)
plt.barh(X, importances, height=0.7, color='#008792', edgecolor='#005344') # 更多颜色可参见颜色大全
plt.xlabel('feature importance',fontname='Times New Roman',fontsize=12) # x 轴
plt.ylabel('features',fontname='Times New Roman',fontsize=12) # y轴
plt.title('Feature Importances') # 标题

'''plt.figure(1,figsize=(8, 4))
i=0
print(len(y))
while i<len(y):  
    #power_smooth = spline(X,y[i],xnew)    
    #plt.grid(True)
    plt.legend(loc='best')
    plt.plot(X,y[i],linewidth=1)#   plt.plot(X,y[i],linewidth=1)原句
    plt.ylabel('Importance')
    plt.xlabel('Feature')
    i=i+1
plt.legend(loc='best')
#plt.savefig('/Users/kqq/Documents/postgraduate/实验图/featureScore', dpi=200)''' 
plt.show()
